//
//  WQCMakePlanViewController.m
//  renxinghuan
//
//  Created by 王晓民 on 2018/3/23.
//  Copyright © 2018年 吴清翠. All rights reserved.
//

#import "WQCMakePlanViewController.h"
#import "configuration.h"
#import "LDCalendarView.h"
#import "NSDate+extend.h"
#import "WYCSelectionPlanController.h"
#import "WYCSeeBillModel.h"
#import "WYCPreviewViewController.h"
#import "passwordView.h"
#import "WYCSelectionModel.h"
#import "WQCPlanPickerView.h"
#import "WQCBankViewController.h"
#import "XLPasswordView.h"
#import "WQCPlanDetailedViewController.h"
#import "WYPopPlanView.h"
@interface WQCMakePlanViewController ()<UITableViewDelegate,UITableViewDataSource,WYCSelectionPlanDelegate,UIPickerViewDataSource,UIPickerViewDelegate,UITextFieldDelegate,XLPasswordViewDelegate,WYPopPlanViewDelegate>
{
     int  summer;
    double string;

}
@property (nonatomic,strong)UIView *maskView;
@property (nonatomic,strong)WYPopPlanView *planView;


@property(nonatomic,strong)NSMutableArray * addArray;
@property(nonatomic,strong)UIView  *payView;
@property(nonatomic,strong)NSString * planID;
@property(strong,nonatomic)NSString  *pass;
@property (nonatomic,weak)WQCPlanPickerView  *bankPickerView;

@property(nonatomic,strong)WYCSelectionModel *bankModel;

@property(nonatomic,strong)WYCSeeBillModel * seeBillModel;

@property(nonatomic,strong)UITableView *makeTableView;
@property (nonatomic, strong)NSMutableArray *seletedDays;//选择的日期
@property (nonatomic, strong)NSMutableArray *titArray;//选择的日期

/** 顶部View */
@property (nonatomic,strong) UIView *topView;

/** 顶部View */
@property (nonatomic,strong) UIView *bigtopView;

@property(nonatomic,strong) UIView  *planList;

@property(nonatomic,strong) UIView  *bgview;

//加号
@property (nonatomic,strong) UIButton  *addButton;
@property (nonatomic, strong)LDCalendarView *calendarView;//日历控件
@property(nonnull,strong) UIPickerView *pickerView;

//减号
@property (nonatomic,strong) UIButton  *rustButton;

//日历
@property (nonatomic,strong) UIButton  *riliButton;

@property (nonatomic,strong) UIButton  *planbtn;

@property (nonatomic,strong)UILabel * staylabel;
@property(nonatomic,strong)UITextField *daylabel;
@property(nonatomic,strong)UITextField *repayTextField;
@property(nonatomic,strong)UITextField *bankTextField;
@property(nonatomic ,strong)UITextField *money;
@property(nonatomic ,strong)UITextField *balance;
@property(nonatomic,strong)UITextField *rili;
@property(nonatomic,strong)UITextField *plan;

@property (nonatomic,strong)WQCPlanPickerView *PView;

@end

@implementation WQCMakePlanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.addArray = [[NSMutableArray alloc]init];
    self.titArray = [[NSMutableArray alloc]init];

    self.navigationController.navigationBarHidden =NO;
    self.view.backgroundColor = WYCGlobalBg;
    [self setupNav];
    UIView  *bgView = [MyUIClass makeUIViewWithFram:CGRectMake(0, NavHeight, kScreenW, 200) andBackColor:wycMain];
    [self.view addSubview:bgView];
    //白色
    UIImageView * backView2 = [[UIImageView alloc] init];
    backView2.frame = CGRectMake(10, 20, ScreenWidth-20, 170);
    backView2.layer.cornerRadius = 30 * px;
    backView2.backgroundColor = [UIColor whiteColor];
    [bgView addSubview:backView2];
    UILabel *benqizhangdan = [[UILabel alloc] init];
   
  
    //总额度
    UILabel *Totallabel = [[UILabel alloc] init];
    Totallabel.frame = CGRectMake(25, 15, 200, 30);
    Totallabel.text = [NSString stringWithFormat:@"%@(%@)",_banknameStr,_lastnumberStr];
    Totallabel.textAlignment = NSTextAlignmentLeft;
    Totallabel.textColor = [UIColor blackColor];
    Totallabel.font = [UIFont boldSystemFontOfSize:15];
    [backView2 addSubview:Totallabel];
    
    UILabel *Total = [[UILabel alloc] init];
    Total.frame = CGRectMake(ScreenWidth-100, 15, 100, 30);
    Total.textAlignment = NSTextAlignmentLeft;
    Total.text = @"信用卡";
    Total.textColor = [UIColor lightGrayColor];
    Total.font = [UIFont systemFontOfSize:15];
    [backView2 addSubview:Total];
    
    
    //总额度
    UILabel *Statementlabel = [[UILabel alloc] init];
    Statementlabel.frame = CGRectMake(0, benqizhangdan.frame.size.height+benqizhangdan.frame.origin.y+100, backView2.width / 3, 50 * px);
    Statementlabel.text = _amountStr;
    Statementlabel.textAlignment = NSTextAlignmentCenter;
    Statementlabel.textColor = [UIColor blackColor];
    Statementlabel.font = [UIFont boldSystemFontOfSize:15];
    [backView2 addSubview:Statementlabel];
    
    
    UILabel *Statement = [[UILabel alloc] init];
    Statement.frame = CGRectMake(0, CGRectGetMaxY(Statementlabel.frame)+10 ,backView2.width / 3 , Statementlabel.height);
    Statement.text = @"总额度";
    Statement.textAlignment = NSTextAlignmentCenter;
    Statement.textColor = [UIColor lightGrayColor];
    Statement.font = [UIFont systemFontOfSize:15];
    [backView2 addSubview:Statement];
    
    //账单日
    UILabel *Repaymentlabel = [[UILabel alloc] init];
    Repaymentlabel.frame = CGRectMake(backView2.width/3, Statementlabel.y, Statementlabel.width, Statementlabel.height);
    Repaymentlabel.text = _billStr;
    Repaymentlabel.textAlignment = NSTextAlignmentCenter;
    Repaymentlabel.textColor = [UIColor blackColor];
    Repaymentlabel.font = [UIFont boldSystemFontOfSize:15];
    [backView2 addSubview:Repaymentlabel];
    
    
    UILabel *Repayment = [[UILabel alloc] init];
    Repayment.frame = CGRectMake(backView2.width/3, CGRectGetMaxY(Repaymentlabel.frame)+10, Statementlabel.width, Statementlabel.height);
    Repayment.text = @"账单日";
    Repayment.textColor = [UIColor lightGrayColor];
    Repayment.textAlignment = NSTextAlignmentCenter;
    Repayment.font = [UIFont systemFontOfSize:15];
    [backView2 addSubview:Repayment];
    
    
    //还款日
    UILabel *Gracelabel = [[UILabel alloc] init];
    Gracelabel.frame = CGRectMake((backView2.width /3)*2, Statementlabel.y, Statementlabel.width, Statementlabel.height);
    Gracelabel.text = _borowStr;
    Gracelabel.textAlignment = NSTextAlignmentCenter;
    Gracelabel.textColor = [UIColor blackColor];
    Gracelabel.font = [UIFont boldSystemFontOfSize:15];
    [backView2 addSubview:Gracelabel];
    
    
    UILabel *Grace = [[UILabel alloc] init];
    Grace.frame = CGRectMake((backView2.width /3)*2, CGRectGetMaxY(Gracelabel.frame)+10, Statementlabel.width, Statementlabel.height);
    Grace.text = @"还款日";
    Grace.textColor = [UIColor lightGrayColor];
    Grace.textAlignment = NSTextAlignmentCenter;
    Grace.font = [UIFont systemFontOfSize:15];
    [backView2 addSubview:Grace];
    NSArray  *imageArr = @[@"还款金额：",@"计划本金：",@"还款天数：",@"消费日期：",@"消费计划："];
    NSArray  *titleArr = @[@"请输入还款金额",@"请输入卡内余额",@"",@"选择还款日期",@"选择消费计划"];
    
    for (int i = 0; i<imageArr.count; i++)
    {
        
        /** 顶部View */
        self.topView = [[UIView alloc] init];
        self.topView.frame = CGRectMake(10,( CGRectGetMaxY(bgView.frame)+10)+60*i, ScreenWidth-20, 50);
        self.topView.userInteractionEnabled =YES;
        self.topView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:self.topView];
        
        UILabel  *title = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
        title.text =[imageArr objectAtIndex:i];
        title.textColor = [UIColor blackColor];
        [self.topView addSubview:title];
        
        /** 输入框*/
        self.repayTextField = [[UITextField alloc] init];
        self.repayTextField.delegate = self;
        self.repayTextField.frame = CGRectMake( 110, 10, _topView.width / 2, 30);
        self.repayTextField.enabled = NO;
        self.repayTextField.keyboardType = UIKeyboardTypeNumberPad;
        self.repayTextField.placeholder = [titleArr objectAtIndex:i];
        self.repayTextField.tintColor = wycMain;
        self.repayTextField.font = [UIFont systemFontOfSize:50 * px];
        [self.topView addSubview:self.repayTextField];
        if (i==0) {
            self.repayTextField.enabled = YES;
            _money = self.repayTextField;
            self.repayTextField.placeholder = @"账单金额加手续费";
//            [self.repayTextField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
            
        }
        if (i==1) {
            self.repayTextField.enabled = YES;
            _balance = self.repayTextField;
            
        }
        if (i==2) {
            _daylabel = self.repayTextField;
            //-号按钮
            self.rustButton = [[UIButton alloc] init];
            self.rustButton.frame = CGRectMake(self.topView.width - 290 * px, 40 * px, 80 * px, 80 * px);
            [self.rustButton setImage:[UIImage imageNamed:@"icon_count_js"] forState:UIControlStateNormal];
            
            [self.rustButton addTarget:self action:@selector(reduceButtonClick) forControlEvents:UIControlEventTouchUpInside];
            
            [self.topView addSubview:self.rustButton];
            
            //+号按钮
            self.addButton = [[UIButton alloc] init];
            self.addButton.frame = CGRectMake(CGRectGetMaxX(self.rustButton.frame) + 40 * px,40 * px, 80 * px, 80 * px);
            [self.addButton setImage:[UIImage imageNamed:@"icon_count_zj"] forState:UIControlStateNormal];
            [self.addButton addTarget:self action:@selector(addButtonClick) forControlEvents:UIControlEventTouchUpInside];
            [self.topView addSubview:self.addButton];
        }
        
        if (i == 3) {
            //日历
            self.riliButton = [[UIButton alloc] init];
            self.riliButton .frame = CGRectMake(110, 40 * px, 180, 40);
            [self.riliButton addTarget:self action:@selector(CalculatorButtonClick) forControlEvents:UIControlEventTouchUpInside];
            [self.topView addSubview:self.riliButton];
            _rili = self.repayTextField;
        }
        if (i == 4) {
            self.planbtn = [[UIButton alloc] init];
            self.planbtn .frame = CGRectMake(110, 40 * px, 180, 40);
            [self.planbtn addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside];
            [self.topView addSubview:self.planbtn];
            _plan = self.repayTextField;
            
        }
    }

    UIButton * backButton = [[UIButton alloc] init];
    backButton.frame = CGRectMake(40, kScreenH-80,  kScreenW-80, 150 * px);
    [backButton addTarget:self action:@selector(returnButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [backButton setTitle:@"预览还款计划" forState:UIControlStateNormal];
    backButton.titleLabel.font = [UIFont systemFontOfSize:BigFont];
    [backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    backButton.layer.cornerRadius = 75 * px;
    backButton.backgroundColor = wycMain;
    [self.view addSubview:backButton];

    
}
- (void)setupNav {
    
    [self.navigationController.navigationBar setBarTintColor:wycMain];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationItem.title = @"创建计划";

}

-(void)pressBtn
{
    
    [self loadPlan];

   
    
    
    
}
- (void)pickerViewhide:(BOOL)hide {
    
    if (hide) {
        
        [UIView animateWithDuration:1 animations:^{
            
            
            self.bankPickerView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, 300);
        }];
    }else {
        
        [UIView animateWithDuration:1 animations:^{
            
            self.bankPickerView.frame = CGRectMake(0, ScreenHeight - 300, ScreenWidth, 300);
        }];
    }
    
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
    [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
    if (_money.text.length&&_balance.text.length) {
        
        int repaymentintValue = [_money.text intValue];
        //卡内余额
        int balanceintValue = [_balance.text intValue];
        
        int onestr = repaymentintValue *0.06 ;
        int str = balanceintValue;
        
        if (str < onestr ) {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"余额不能低于的6%"];
            
            
            return;
        }
        double mo = [_money.text doubleValue];
        double str2 = [[WYCAccountTool unarchiverepaymentRate] doubleValue];
        
        string = (mo * str2) / 100;
        
        double  dascount = repaymentintValue / balanceintValue - string;
        
        NSString * stringDday = [NSString stringWithFormat:@"%.2f",dascount];
        
        NSArray * dayarray = [stringDday componentsSeparatedByString:@"."];
        if (dayarray[1] != 0) {
            double sm = [dayarray[0] intValue] + 1;
            double i =  (balanceintValue - string - sm) * 1;
            double n = repaymentintValue / i;
            NSString * nStr = [NSString stringWithFormat:@"%.2f",n];
            NSArray * narr = [nStr componentsSeparatedByString:@"."];
            
            if (narr[1] != 0) {
                summer = [narr[0] intValue] + 1;
                _daylabel.text = [NSString stringWithFormat:@"%d",summer];
            }
            
        }else{
            summer = [dayarray[0] intValue];
            _daylabel.text = [NSString stringWithFormat:@"%d",summer];
            
        }
    
        
    }
    return;
    if (_money.text.length == 0) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"还款金额不能为空"];
        
        
        return;
    }
    else if (_balance.text.length == 0) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"卡内金额不能为空"];
        
        return;
    }
    
    else  if (textField == _balance) {
        NSLog(@"进来了");
        int repaymentintValue = [_money.text intValue];
        //卡内余额
        int balanceintValue = [_balance.text intValue];
        
        int onestr = repaymentintValue *0.06 ;
        int str = balanceintValue;
        
        if (str < onestr ) {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"余额不能低于的6%"];
            
            
            return;
        }
        double mo = [_money.text doubleValue];
        double str2 = [[WYCAccountTool unarchiverepaymentRate] doubleValue];
        
        string = (mo * str2) / 100;
        
        double  dascount = repaymentintValue / balanceintValue - string;
        
        NSString * stringDday = [NSString stringWithFormat:@"%.2f",dascount];
        
        NSArray * dayarray = [stringDday componentsSeparatedByString:@"."];
        if (dayarray[1] != 0) {
            double sm = [dayarray[0] intValue] + 1;
            double i =  (balanceintValue - string - sm) * 1;
            double n = repaymentintValue / i;
            NSString * nStr = [NSString stringWithFormat:@"%.2f",n];
            NSArray * narr = [nStr componentsSeparatedByString:@"."];
            
            if (narr[1] != 0) {
                summer = [narr[0] intValue] + 1;
                _daylabel.text = [NSString stringWithFormat:@"%d",summer];
            }
            
        }else{
            summer = [dayarray[0] intValue];
            _daylabel.text = [NSString stringWithFormat:@"%d",summer];
            
        }
    }
}
#pragma make --------->>减号按钮
- (void)reduceButtonClick{
    _rili.text =@"";
    _plan.text =@"";
    summer = [_daylabel.text intValue];
    if (summer == 0) {
        return;
    }
    summer--;
    _daylabel.text = [NSString stringWithFormat:@"%d",summer];
    
    
}
#pragma make --------->>加号按钮
- (void)addButtonClick{
    _rili.text =@"";
    _plan.text =@"";
    summer = [_daylabel.text intValue];
    summer++;
    _daylabel.text = [NSString stringWithFormat:@"%d",summer];
}

#pragma make --------->>日历选择
- (void)CalculatorButtonClick{
    
    
    int huankuan = [_repayTextField.text intValue];
    //卡内余额
    int balanceintValue = [_bankTextField.text intValue];
    
    int onestr = huankuan *0.06 ;
    int str = balanceintValue;
    
    if (str < onestr ) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"余额不能低于还款金额的6%"];
      
        return;
    }else{
        if (!_calendarView) {
            _calendarView = [[LDCalendarView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT)];
            _calendarView.startDay = self.billStr;
            _calendarView.endDay = self.borowStr;
            [self.view addSubview:_calendarView];
            
            __weak typeof(self) weakSelf = self;
            _calendarView.complete = ^(NSArray *result) {
                if (result.count != 0) {
                    weakSelf.seletedDays = [result mutableCopy];
                    NSString * str = [NSString stringWithFormat:@"%@",weakSelf.showStr];
                    NSString *b = [str substringToIndex:[str length]-1];
                    
                    if (![_daylabel.text isEqualToString:[NSString stringWithFormat:@"%lu",(unsigned long)_seletedDays.count]] ) {
                        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"所选天数有误"];
                    }
                   _rili.text = b;
                    
                }
            };
        }
        [self.calendarView show];
    }
}
- (NSString *)showStr {
    NSMutableString *str = [NSMutableString string];
    
    [str appendString:@""];
    //从小到大排序
    [self.seletedDays sortUsingComparator:^NSComparisonResult(NSNumber *obj1, NSNumber *obj2) {
        return [obj1 compare:obj2];
    }];
    for (NSNumber *interval in self.seletedDays) {
        NSString *partStr = [NSDate stringWithTimestamp:interval.doubleValue/1000.0 format:@"yyyy-MM-dd"];
        [str appendFormat:@"%@,",partStr];
        [self.makeTableView reloadData];
    }
    return [str copy];
}

//预览还款计划
- (void)returnButtonClick{

    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
    [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
    if (_money.text.length == 0) {
               [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"还款金额不能为空"];
       
        
        return;
    }
    else if (_balance.text.length == 0) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"卡内金额不能为空"];
    
        return;
    }
   else if (![_daylabel.text isEqualToString:[NSString stringWithFormat:@"%lu",(unsigned long)_seletedDays.count]] ) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"所选天数有误"];
     
       return;
    }
    else if (self.plan.text.length == 0) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"消费计划不能为空"];
      
        return;
    }
    else{
        
                NSMutableDictionary * params = [NSMutableDictionary dictionary];
                params[@"cardId"] = self.cardIdStr;
                params[@"repaymentMoney"] =_money.text;
                params[@"cardBalance"] = _balance.text;
                params[@"repaymentNum"] = _daylabel.text;
                params[@"repaymentDay"] = _rili.text;
                params[@"planId"] = self.planID;
                NSLog(@"params%@",params);
        [HttpTool POST:createPlan params:params success:^(NSURLSessionDataTask *task, id responseObject) {
            
            NSLog(@"预览计划%@",responseObject);
            if ([[responseObject objectForKey:@"code"] intValue] == 0)
            {
                
               NSDictionary * result = responseObject[@"result"];
                NSDictionary  *dic = result[@"plan"];
                NSString  *balance = dic[@"balance"];
                NSString  *days = dic[@"days"];
                NSString  *amount = dic[@"amount"];
                NSString  *totalConsumeNum = dic[@"totalConsumeNum"];
                NSString  *totalCharge = dic[@"totalCharge"];
                
                [_titArray addObject:[NSString stringWithFormat:@"%.2f",[amount doubleValue]]];
                [_titArray addObject:[NSString stringWithFormat:@"%@天",days]];
                [_titArray addObject:[NSString stringWithFormat:@"%.2f",[balance doubleValue]]];
                [_titArray addObject:[NSString stringWithFormat:@"%@",totalConsumeNum]];
                [_titArray addObject:[NSString stringWithFormat:@"%.2f",[totalCharge doubleValue]]];

                [self creatBorrow];
            
            }
            else
            {
                [SVProgressHUD showImage:nil status:responseObject[@"message"]];
            }
            
        } fail:^(NSURLSessionDataTask *task, NSError *error) {
            
        }];


//

        
    }
}
/**
 *  点击了忘记密码时调用
 */
- (void)passwordViewClickForgetPassword:(XLPasswordView *)passwordView
{
    NSLog(@"点击了忘记密码,在这里做一些事情");
    _bgview.hidden=YES;
    
    //未设置支付密码
    WQCBankViewController  *codeView = [[WQCBankViewController alloc]init];
    [self.navigationController pushViewController:codeView animated:YES];
}
- (UIWindow *)lastWindow
{
    NSArray *windows = [UIApplication sharedApplication].windows;
    for(UIWindow *window in [windows reverseObjectEnumerator]) {
        
        if ([window isKindOfClass:[UIWindow class]] &&
            CGRectEqualToRect(window.bounds, [UIScreen mainScreen].bounds))
            
            return window;
    }
    
    return [UIApplication sharedApplication].keyWindow;
}

-(void)creatBorrow
{
    
    _bgview = [MyUIClass makeUIViewWithFram:CGRectMake(0, 0, ScreenWidth, ScreenHeight) andBackColor:nil];
    _bgview.backgroundColor=[UIColor  colorWithRed:40/255.0 green:42/255.0 blue:54/255.0 alpha:0.5];
    [self.lastWindow addSubview:_bgview];
    
    _planList = [MyUIClass makeUIViewWithFram:CGRectMake(0, ScreenHeight-320, ScreenWidth, 320) andBackColor:[UIColor whiteColor]];
    _planList.userInteractionEnabled =YES;
   [_bgview addSubview:_planList];
    
    UILabel  *tit = [MyUIClass makeUILabelWithFrame:CGRectMake(10, 10, ScreenWidth-20, 20) andBackColor:[UIColor clearColor] andText:@"还款计划" andTextColor:[UIColor blackColor] andFont:[UIFont boldSystemFontOfSize:18] andAlignment:NSTextAlignmentCenter];
    [_planList addSubview:tit];
    
    UIButton * cancelButton =[UIButton buttonWithType:UIButtonTypeCustom];
    cancelButton.frame = CGRectMake(5, 0, 64, 44);
    [cancelButton setImage:[UIImage imageNamed:@"Nav_close"] forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(cancelButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [_planList addSubview:cancelButton];
    
    UIButton  *btnM = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnM setTitle:@"计划明细" forState:UIControlStateNormal];
    [btnM setTitleColor:wycMain forState:UIControlStateNormal];
    btnM .frame =CGRectMake(ScreenWidth-120, 10, 120, 20);
    [btnM addTarget:self action:@selector(pressPlan) forControlEvents:UIControlEventTouchUpInside];
    [_planList addSubview:btnM];
    UIView  *line = [MyUIClass makeUIViewWithFram:CGRectMake(0, 40, ScreenWidth, 1) andBackColor:WYCGlobalBg];
    [_planList addSubview:line];
    NSArray  *imageArr = @[@"实际还款金额：",@"还款天数：",@"卡内余额：",@"还款期数：",@"总手续费："];
    
    for (int i = 0; i<imageArr.count; i++)
    {
        UILabel  *title = [[UILabel alloc]initWithFrame:CGRectMake(10, 60+40*i, 130, 30)];
        title.text =[imageArr objectAtIndex:i];
        title.textColor = [UIColor grayColor];
        [_planList addSubview:title];
        UILabel  *titlestr = [[UILabel alloc]initWithFrame:CGRectMake(120, 60+40*i, 250, 30)];
        
        titlestr.text =[NSString stringWithFormat:@"%@",[self.titArray objectAtIndex:i]];
        titlestr.textColor = [UIColor blackColor];
        [_planList addSubview:titlestr];
    }
    
    UIButton * backButton = [[UIButton alloc] init];
    backButton.frame = CGRectMake(40, _planList.height-60,  kScreenW-80, 40);
    [backButton addTarget:self action:@selector(clickimplement) forControlEvents:UIControlEventTouchUpInside];
    [backButton setTitle:@"执行还款计划" forState:UIControlStateNormal];
    backButton.titleLabel.font = [UIFont systemFontOfSize:BigFont];
    [backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    backButton.layer.cornerRadius = 75 * px;
    backButton.backgroundColor = wycMain;
    [_planList addSubview:backButton];
}
//计划明细
-(void)pressPlan
{
    _bgview.hidden =YES;
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"cardId"] = self.cardIdStr;
    params[@"repaymentMoney"] =_money.text;
    params[@"cardBalance"] = _balance.text;
    params[@"repaymentNum"] = _daylabel.text;
    params[@"repaymentDay"] = _rili.text;
    params[@"planId"] = self.planID;
    NSLog(@"params%@",params);
    WYCPreviewViewController  *prev= [[WYCPreviewViewController alloc]init];
    prev.parmDic =params;
    [self.navigationController pushViewController:prev animated:YES];
}
//取消计划按钮
-(void)cancelButtonClick
{
  //  [self.lastWindow removeFromSuperview];
//   [_planList removeFromSuperview];
    [_bgview removeFromSuperview];
}
//执行还款计划
-(void)clickimplement
{
  
    if ([[WYCAccountTool unarchivehavePayPassword] isEqualToString:@"1"])
    {
        _bgview.hidden=YES;

        //未设置支付密码
        WQCBankViewController  *codeView = [[WQCBankViewController alloc]init];
        [self.navigationController pushViewController:codeView animated:YES];
        
    }
    else
    {
        _bgview.hidden = YES;
        _planList.hidden = YES;
        
        XLPasswordView *passwordView = [XLPasswordView passwordViewWithKeyboardType:XLPasswordViewKeyboardTypeSystem];
        passwordView.delegate = self;
        [passwordView showPasswordInView:self.view];
        [passwordView.passwordInputView.textField becomeFirstResponder];

    }
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

#pragma mark    -   XLPasswordViewDelegate

/**
 *  输入密码位数已满时调用
 */
- (void)passwordView:(XLPasswordView *)passwordView didFinishInput:(NSString *)password
{
    NSLog(@"输入密码位数已满,在这里做一些事情,例如自动校验密码%@",password);
    
               [self pressPlan:password];

}

//计划明细
-(void)pressPlan:(NSString *)pass
{
    //RSA密码加密
    NSString *pas = [RSA encryptString:pass publicKey:RSA_P_KEY];
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"payPassword"] = pas ;
    
    [HttpTool POST:beginPlan params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSLog(@"计划创建返回信息%@ %@",responseObject,params);
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"计划创建成功"];
           
            UIViewController *viewCtl = self.navigationController.viewControllers[1];
            
            [self.navigationController popToViewController:viewCtl animated:YES];
            
        }
        else{
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:responseObject[@"message"]];
            [self.payView removeFromSuperview];
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}
- (void)loadPlan {
    
    if (self.money.text.length == 0) {
        [SVProgressHUD showImage:nil status:@"还款金额不能为空"];
        return;
    }
    else if (self.daylabel.text.length ==0)
    {
        [SVProgressHUD showImage:nil status:@"天数不能为空"];
        return;

    }
    else if (self.balance.text.length==0)
    {
        [SVProgressHUD showImage:nil status:@"卡内余额不能为空"];
        return;

    }else
    {
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"cardId"] = _cardIdStr;
    params[@"repaymentMoney"] = _money.text;
    params[@"repaymentNum"] =_daylabel.text;
    params[@"cardBalance"] = _balance.text;
 
    NSLog(@"消费计划%@",params);
    [HttpTool POST:checkPlan params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"消费计划返回%@",responseObject);
        [self.addArray removeAllObjects];
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            NSArray * resultArray = responseObject[@"result"];
            for (NSDictionary * dict in resultArray) {
                
                WYCSelectionModel * selectionModel = [WYCSelectionModel mj_objectWithKeyValues:dict];
                [self.addArray addObject:selectionModel];
//                [self.addArray addObject:selectionModel];
//                [self.addArray addObject:selectionModel];
//                [self.addArray addObject:selectionModel];
            }
            if (self.addArray.count != 0) {
                NSLog(@"有数据");
                WYCSelectionModel * model = self.addArray[0];
                model.isSelect = YES;
//                WQCPlanPickerView *bankView = [[WQCPlanPickerView alloc]initWithSourceData:self.addArray confirmBlock:^(WYCSelectionModel *bankModel) {
//                    _bankModel = bankModel;
//                    _plan.text = [NSString stringWithFormat:@"日消费%@笔",bankModel.maxNumber];
//                    self.planID = bankModel.planId;
//                    [self pickerViewhide:YES];
//
//                } cancelBlock:^(WYCSelectionModel *bankModel)
//                                               {
//                                                   _plan.text = bankModel.maxAmount;
//                                                   [self pickerViewhide:YES];
//
//
//                                               }];
//                self.PView = bankView;
                WYPopPlanView *popView = [[WYPopPlanView alloc]init];
                popView.delegate=self;
                self.planView = popView;
                popView.dataArray = self.addArray;
                popView.frame = CGRectMake(0, ScreenHeight-300, ScreenWidth, 300);
                [[UIApplication sharedApplication].keyWindow addSubview:self.maskView];
                [[UIApplication sharedApplication].keyWindow addSubview:popView];
              //  [self.view addSubview:popView];
              //  self.bankPickerView = popView;
              //  [self.view addSubview:self.bankPickerView];
              //  [self pickerViewhide:NO];
                
            }else{
                [SVProgressHUD showImage:nil status:@"暂无匹配数据"];

            }
            [self.PView refreshWithSourceData:self.addArray];
        }else{
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
            [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
            [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
            
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    }
    
}

-(void)clickPlan:(WYCSelectionModel *)model
{
    
    [self exit:nil];
    _bankModel = model;
    _plan.text = [NSString stringWithFormat:@"日消费%@笔",model.maxNumber];
    self.planID = model.planId;
    
}


- (void)setDelegatemaxNumber:(NSString *)maxNumber setID:(NSString *)ID{
    NSString *strPlan = [NSString stringWithFormat:@"%@",maxNumber];
//    self.plan.text = strPlan;
        [self.planbtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.planID = ID;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(UIView *)maskView
{
    if (!_maskView) {
        _maskView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _maskView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        _maskView.alpha = 0.5f;
        UIButton *button=[[UIButton alloc]init];
        [_maskView addSubview:button];
        button.frame=_maskView.bounds;
        [button addTarget:self action:@selector(exit:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _maskView;
}
-(void)exit:(UIButton *)btn
{
    [self.maskView removeFromSuperview];
    [self setMaskView:nil];
    [self.planView removeFromSuperview];
    [self setPlanView:nil];
//    [[UIApplication sharedApplication].keyWindow addSubview:_maskView];
}
@end
